﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using UnityEngine;
using System.Threading;

namespace Common
{
    public class AssetHelp
    {
        /// <summary>
        /// far layer define
        /// </summary>
        public const int FarLayer = 8;
        /// <summary>
        /// near layer define
        /// </summary>
        public const int NearLayer = 9;
        /// <summary>
        /// player layer define
        /// </summary>
        public const int PlayerLayer = 10;
        /// <summary>
        /// hidden layer define
        /// </summary>
        public const int HiddenLayer = 11;
        /// <summary>
        /// default layer define
        /// </summary>
        public const int DefaultLayer = 0;
        /// <summary>
        /// near layer distance
        /// </summary>
        public static float NearLayerDistance = 50;
        /// <summary>
        /// far layer distance
        /// </summary>
        public static float FarLayerDistance = 0;
        /// <summary>
        /// player layer distance
        /// </summary>
        public static float PlayerLayerDistance = 50;
        /// <summary>
        /// player layer distance
        /// </summary>
        public static float DefaultLayerDistance = 100;
        public static void SetLayerDistance()
        {
            float[] distance = new float[32];
            distance[FarLayer] = FarLayerDistance;//far
            distance[DefaultLayer] = DefaultLayerDistance;//default
            distance[NearLayer] = NearLayerDistance;//near
            distance[PlayerLayer] = PlayerLayerDistance;//player
            Define.MainCamera.layerCullDistances = distance;
        }
        public static Transform FindChildObject(string partName, Transform mod, string partPath)
        {
            Transform Result = null;
            if (partPath == null)
            {
                partPath = mod.name;
            }
            if (mod.childCount > 0)
            {
                for (int i = 0; i < mod.childCount; i++)
                {
                    Transform item = mod.GetChild(i);
                    if (item.name == partName)
                    {
                        Result = item;
                        break;
                    }
                    else
                    {
                        Result = FindChildObject(partName, item, string.Concat(new string[] { partPath, "/", item.name }));
                        if (Result != null)
                        {
                            break;
                        }
                    }
                }
            }
            return Result;
        }
        public static Vector2 GetFontWidth(GUIStyle style, string str)
        {
            Vector2 size = Vector2.zero;
            if (style != null)
            {
                size = style.CalcSize(new GUIContent(str));
                //size = style.CalcScreenSize(style.CalcSize(new GUIContent(str)));
            }
            return size;
        }
        public static int GetPlayerId(string rootName)
        {
            int id = 0;
            rootName = rootName.Replace(Define.PlayerRootFirstName, "");
            int.TryParse(rootName, out id);
            return id;
        }
        public static void CheckSocketPolicy(string ip, int port, Action<bool> callBack)
        {
            if (Application.isWebPlayer || Application.isEditor)
            {
                Log.Info("CheckSocketPolicy begin...");
                Thread t = new Thread(delegate()
                {
                    bool result = Security.PrefetchSocketPolicy(ip, port);
                    Log.Info("CheckSocketPolicy end:" + result);
                    callBack(result);
                });
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                callBack(true);
            }
            //callBack(true);
        }
        public static bool IsFront(IUnit owner, IUnit other)
        {
            bool result = false;
            if (owner.Direction == MovementType.Left)
            {
                result = other.Position.z < +owner.Position.z; 
            }
            else if (owner.Direction == MovementType.Right)
            {
                result = other.Position.z >= owner.Position.z; 
            }
            return result;
        }
        /*
        public static float String2Float(string s)
        {
            float result = 0;
            float.TryParse(s, NumberStyles.Float, culture, out result);
            return result;
        }
        public static string Float2String(float f)
        {
            return f.ToString("f3", culture);
        }
        public static string Float2String(float f, string format)
        {
            return f.ToString(format, culture);
        }
        public static string Vector2String(Vector3 value)
        {
            return Vector2String(value, "f3");
        }
        public static string Vector2String(Vector3 value, string format)
        {
            return Float2String(value.x, format) + "," + Float2String(value.y, format) + "," + Float2String(value.z, format);
        }
        private static CultureInfo culture = new CultureInfo("en-US");
         * */
    }

}
